﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace DusonFramework.Core.Web.Web.HttpModules
{
    public class UrlRewrite : IHttpModule
    {
        #region IHttpModule Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            // Nothing to dispose
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        #endregion

        /// <summary>
        /// Handles the BeginRequest event of the context control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            string path = context.Request.Path.ToUpperInvariant();
            string url = context.Request.RawUrl.ToUpperInvariant();

            if (context.Request.Path.Contains(".aspx"))
            {
                context.RewritePath("default.aspx?blog=true" + GetQueryString(context));
            }
        }

        private static readonly Regex YEAR = new Regex("/([0-9][0-9][0-9][0-9])/", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private static readonly Regex YEAR_MONTH = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])/", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private static readonly Regex YEAR_MONTH_DAY = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9])/", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        private static void RewriteDefault(HttpContext context)
        {
            string url = context.Request.RawUrl;
            string page = "&page=" + context.Request.QueryString["page"];
            if (string.IsNullOrEmpty(context.Request.QueryString["page"]))
                page = null;

            if (YEAR_MONTH_DAY.IsMatch(url))
            {
                Match match = YEAR_MONTH_DAY.Match(url);
                string year = match.Groups[1].Value;
                string month = match.Groups[2].Value;
                string day = match.Groups[3].Value;
                string date = year + "-" + month + "-" + day;
                context.RewritePath("~/default.aspx?date=" + date + page, false);
            }
            else if (YEAR_MONTH.IsMatch(url))
            {
                Match match = YEAR_MONTH.Match(url);
                string year = match.Groups[1].Value;
                string month = match.Groups[2].Value;
                string path = string.Format("default.aspx?year={0}&month={1}", year, month);
                context.RewritePath("~/" + path + page, false);
            }
            else if (YEAR.IsMatch(url))
            {
                Match match = YEAR.Match(url);
                string year = match.Groups[1].Value;
                string path = string.Format("default.aspx?year={0}", year);
                context.RewritePath("~/" + path + page, false);
            }
        }

        private static string GetQueryString(HttpContext context)
        {
            string query = context.Request.QueryString.ToString();
            if (!string.IsNullOrEmpty(query))
                return "&" + query;

            return string.Empty;
        }
    }
}
